﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Obra.SmugMug.Api;

namespace Obra.PowerShell.SmugMug
{
    internal class ImageCmdletServiceProvider :
        ICmdletService
    {
        private readonly PathSegments pathSegments;
        private readonly ISmugMugClientService smugMugClient;
        private readonly IPathMappingService pathMappingService;

        /// <summary>
        /// c-tor
        /// </summary>
        public ImageCmdletServiceProvider(
            PathSegments pathSegments,
            ISmugMugClientService smugMugClient,
            IPathMappingService pathMappingService)
        {
            this.pathSegments = pathSegments;
            this.smugMugClient = smugMugClient;
            this.pathMappingService = pathMappingService;
        }

        #region ICmdletService Members

        /// <summary>
        /// User specified command line options.
        /// </summary>
        public DynamicParametersItem Options
        {
            get;
            set;
        }

        public string Name
        {
            get { return this.pathSegments.ImageName; }
        }

        public string Path
        {
            get { return this.pathSegments.Path; }
        }

        public void Clear()
        {
            throw new NotImplementedException();
        }

        public void Copy(string path)
        {
            throw new NotImplementedException();
        }

        public bool Exists()
        {
            return this.GetImage() != null;
        }

        public object Get()
        {
            return this.GetImage();
        }

        public Uri GetUri()
        {
            return this.GetImage().MediumUrl;
        }

        public DirectoryChild[] GetChildren()
        {
            throw new NotImplementedException();
        }

        public bool IsContainer()
        {
            return false;
        }

        public void Move(string path)
        {
            Album album = this.pathMappingService.ToCmdletService(path).Get() as Album;
            if (album == null)
            {
                throw new ArgumentException();
            }

            var image = this.GetImage();

            if (album.Id != image.AlbumId)
            {
                this.smugMugClient.ChangeImageAlbum(
                    image.Id,
                    album.Id);
            }
        }

        public void New(string name, object value)
        {
            throw new NotImplementedException();
        }

        public void Remove()
        {
            this.smugMugClient.DeleteImage(this.GetImage().Id);
        }

        public void Rename(string name)
        {
            throw new NotImplementedException();
        }

        public void Set(object value)
        {
            throw new NotImplementedException();
        }

        #endregion

        private Image GetImage()
        {
            // disambiguate the image name if necessary
            AlbumContract albumContract = this.smugMugClient.GetAlbums().Albums.SingleOrDefault(
                x => x.Name == this.pathSegments.AlbumName);

            if (albumContract == null)
            {
                return null;
            }

            ImageContract imageContract = this.smugMugClient.GetImages(albumContract.Id).Images.SingleOrDefault(
                x => x.Name == this.pathSegments.ImageName);

            if (imageContract == null)
            {
                return null;
            }

            return new Image(
                imageContract,
                this.smugMugClient.Session,
                this.smugMugClient);
        }
    }
}
